$(function() {
    var addressCookie=JSON.parse(CommonJs.cookie.get('address')?CommonJs.cookie.get('address'):'{}');
    CommonJs.address.callBack=function () {
        setAddress();
    }
    function  setAddress() {
        var selectAddress=$(".select-address");
        if(CommonJs.cookie.get('address')){
            addressCookie=JSON.parse(CommonJs.cookie.get('address'));

            selectAddress.html(
                addressCookie.ua_province+
                addressCookie.ua_city+
                addressCookie.ua_county+
                addressCookie.ua_street
            );
        }
    }
    setAddress();
    //时间控件lCalendar
    new lCalendar().init({
        'trigger': '#Hr_BookInstall_PruchaseTime',
        'type': 'date'
    });
    new lCalendar().init({
        'trigger': '#BaoXiu_Hr_BookInstall_PruchaseTime',
        'type': 'date'
    });
    new lCalendar().init({
        'trigger': '#Hr_BookInstall_PruchaseDate',
        'type': 'time'
    });
    new lCalendar().init({
        'trigger': '#BaoXiu_Hr_BookInstall_PruchaseDate',
        'type': 'time'
    });
    var postStart = true;
    $("#baozhuang").click(function (e) {
        e.preventDefault();
        var msg=false,
            pid=window.location.href.getQueryJson().pid;
        if(postStart) {
            postStart = false;
            if (!$('#Hr_BookInstall_tel').val().isMobile()) {
                msg = '请填写正确的手机号';
            }
            if (!$('#Hr_BookInstall_PruchaseTime').val()) {
                msg = '请选择上门日期';
            }
            if (!$('#Hr_BookInstall_PruchaseDate').val()) {
                msg = '请选择上门时间';
            }
            if (msg) {
                CommonJs.alert.show({
                    model: 'error',
                    title: msg
                });
                postStart = true;
            } else {
                $.ajax({
                    type: "POST",
                    url: "/request-conv/index.php?m=BookInstall&a=postInstall",
                    dataType: 'json',
                    data: {
                        "p_id": pid ? pid : CommonJs.cookie.get('p_id'),
                        "u_id": CommonJs.cookie.get('u_id'),
                        "ps_address_id": addressCookie.ua_id,
                        "ps_address": addressCookie.ua_province ? (addressCookie.ua_province +
                        addressCookie.ua_city +
                        addressCookie.ua_county +
                        addressCookie.ua_street) : '',
                        "ps_mobile": $('#Hr_BookInstall_tel').val(),
                        "ps_server_date": $('#Hr_BookInstall_PruchaseTime').val(),
                        "ps_server_time": $('#Hr_BookInstall_PruchaseDate').val(),
                        "ps_remark": $('#AppointmentInstall_Address_Detail').val()
                    },
                    success: function (result) {
                        postStart = true;
                        if (result.Code == 200) {
                            var alt;
                            if (CommonJs.cookie.get('u_id')) {
                                alt = CommonJs.alert.show(Model_Date_Star.Success[4].login);
                            } else {
                                alt = CommonJs.alert.show(Model_Date_Star.Success[4].notLogin);
                            }
                            alt.params.pane.find('.Model_Closed').click(function () {
                                window.history.go(-1);
                            });
                        } else {
                            CommonJs.alert.show({
                                model: 'error',
                                title: result.erMsg
                            });
                        }
                    },
                    error:function () {
                        postStart = true;
                    }
                });
            }
        }
    });
    $("#baoxiu").click(function (e) {
        e.preventDefault();
        var msg=false,
            pid=window.location.href.getQueryJson().pid;

        if(postStart) {
            postStart = false;
            if (!$('#BaoXiu_Hr_BookInstall_tel').val().isMobile()) {
                msg = '请填写正确的手机号';
            }
            if (!$('#BaoXiu_Hr_BookInstall_PruchaseTime').val()) {
                msg = '请选择上门日期';
            }
            if (!$('#BaoXiu_Hr_BookInstall_PruchaseDate').val()) {
                msg = '请选择上门时间';
            }
            if (msg) {
                CommonJs.alert.show({
                    model: 'error',
                    title: msg
                });
                postStart = true;
            } else {
                $.ajax({
                    type: "POST",
                    url: "/request-conv/index.php?m=BookInstall&a=postRepair",
                    dataType: 'json',
                    data: {
                        "p_id": pid ? pid : CommonJs.cookie.get('p_id'),
                        "u_id": CommonJs.cookie.get('u_id'),
                        "ps_address_id": addressCookie.ua_id,
                        "ps_address": addressCookie.ua_province ? (addressCookie.ua_province +
                        addressCookie.ua_city +
                        addressCookie.ua_county +
                        addressCookie.ua_street) : '',
                        "ps_mobile": $('#BaoXiu_Hr_BookInstall_tel').val(),
                        "ps_server_date": $('#BaoXiu_Hr_BookInstall_PruchaseTime').val(),
                        "ps_server_time": $('#BaoXiu_Hr_BookInstall_PruchaseDate').val(),
                        "ps_remark": $('#BaoXiu_AppointmentInstall_Address_Detail').val()
                    },
                    success: function (result) {
                        postStart = true;
                        if (result.Code == 200) {
                            if (CommonJs.cookie.get('u_id')) {
                                var alt = CommonJs.alert.show(Model_Date_Star.Success[4].login);
                            } else {
                                var alt = CommonJs.alert.show(Model_Date_Star.Success[4].notLogin);
                            }
                            alt.params.pane.find('.Model_Closed').click(function () {
                                window.history.go(-1);
                            });
                        } else {
                            CommonJs.alert.show({
                                model: 'error',
                                title: result.erMsg
                            });
                        }
                    },
                    error:function () {
                        postStart = true;
                    }
                });
            }
        }
    });
});